<template>
	<view>
		<group-list :groupData="listData"></group-list>
	</view>
</template>

<script>
	import groupList from '@/components/group-list.vue'
	import api from '@/api/index.js'
	export default {
		data() {
			return {
				t: 0,
				page: 1,
				listData: {
					type_id: null,
					type_name: '',
					list: []
				},
				search: ''
			}
		},
		components: {
			groupList
		},
		methods: {
			async load(pg, t, search) {
				if (search) {
					var {
						type_name,
						type_id,
						list,
						pagecount
					} = await api.searchData(pg, search)
				} else {
					var {
						type_name,
						type_id,
						list,
						pagecount
					} = await api.listData(pg, t)
				}
				this.listData.type_id = type_id
				this.listData.type_name = type_name
				this.listData.list = this.listData.list.concat(list)
				if (pg > pagecount) {
					if (this.listData.list.length < 1) {
						uni.showToast({
							image:'../../static/images/fail.png',
							title: '没有匹配结果',
							duration: 2000
						});
					} else {
						uni.showToast({
							title: '已是最后一页',
							duration: 2000
						});
					}
					this.page = pagecount
				}
			},
			async reload(pg, t, search) {
				if (search) {
					var {
						type_name,
						type_id,
						list
					} = await api.searchData(pg, search)
				} else {
					var {
						type_name,
						type_id,
						list
					} = await api.listData(pg, t)
				}

				this.listData.type_id = type_id
				this.listData.type_name = type_name
				this.listData.list = list
			}
		},
		onLoad(option) {
			this.t = option.id
			this.search = option.search
			this.load(this.page, this.t, this.search)
		},
		onPullDownRefresh() {
			this.page = 1
			this.reload(this.page, this.t, this.search)
			uni.stopPullDownRefresh();
		},
		onReachBottom() {
			this.page = this.page + 1
			this.load(this.page, this.t, this.search)
		}
	}
</script>

<style>

</style>
